﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class InvoiceController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/Invoice
        public IEnumerable<HoaDon> GetHoaDons()
        {
            var hoadons = db.HoaDons.Include(h => h.TaiKhoan);
            return hoadons.AsEnumerable();
        }

        // GET api/Invoice/5
        public HoaDon GetHoaDon(string id)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            if (hoadon == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return hoadon;
        }

        // PUT api/Invoice/5
        public HttpResponseMessage PutHoaDon(string id, HoaDon hoadon)
        {
            if (ModelState.IsValid && id == hoadon.MaHoaDon)
            {
                db.Entry(hoadon).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Invoice
        public HttpResponseMessage PostHoaDon(HoaDon hoadon)
        {
            if (ModelState.IsValid)
            {
                db.HoaDons.Add(hoadon);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, hoadon);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = hoadon.MaHoaDon }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Invoice/5
        public HttpResponseMessage DeleteHoaDon(string id)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            if (hoadon == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.HoaDons.Remove(hoadon);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, hoadon);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}